<?php

namespace app\api\controller;
use think\Controller;
use think\Db;

class UserTpls extends Base
{
    public function __construct(){
        parent::__construct();
        $this->user=session('user');
    }

    //获取用户模板
    public function getUserTpls($name='',$type='',$pageNum=1,$pageSize=20)
    {
        $where=[];
        if($name!=''){
            $where['name']=['like','%'.$name.'%'];
        }
        if($type!=''){
            $where['type']=$type;
        }
        $where['owner']=$this->user['id'];
        $data = Db::name('tpls_user')->where($where)->page($pageNum,$pageSize)->select();
        $count = Db::name('tpls_user')->where($where)->count();
        return json(['code'=>200,'msg'=>'成功','data'=>$data,'count'=>$count,'success'=>true]);
    }
    //删除用户模板
    public function delUserTpls($id='')
    {
        if($id==''){
            return result(500,'id参数不能为空');
        }

        $where=[];
        $where['id']=$id;
        $where['owner']=$this->user['id'];

        Db::name('tpls_user')->where($where)->delete();
        return result(200,'删除成功');
    }
    //添加用户模板
    public function addUserTpls()
    {
        $params=input('post.');
        $data=[
            'name'=>$params['name'],
            'data'=>$params['data'],
            'pic'=>$params['pic'],
            'owner'=>$this->user['id'],
        ];

        $id = Db::name('tpls_user')->insertGetId($data);
        $data=Db::name('tpls_user')->where(['id'=>$id])->find();
        return result(200,'成功',$data);
    }
}